package com.wzy.oa_sys.controller.login;

import com.wzy.oa_sys.model.request.login.LoginRequest;
import com.wzy.oa_sys.model.response.login.LoginResponse;
import com.wzy.oa_sys.service.login.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private LoginService loginService;


    // 登录请求处理
    @PostMapping("/signin") // 处理 POST 请求到 /login/signin
    public ResponseEntity<?> login(@RequestBody LoginRequest request) {
        String username = request.getUsername();
        String password = request.getPassword();

        // 验证用户
        if (loginService.validateUser(username, password)) {
            String token = loginService.generateToken(username); // 生成 token
            System.out.println("token: " + token);
            return ResponseEntity.ok(new LoginResponse(true, token, "登录成功"));
        } else {
            System.out.println("用户名或密码不正确");
            return ResponseEntity.status(401).body(new LoginResponse(false, null, "用户名或密码不正确"));
        }
    }
}
